import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os

def filter_and_plot():
    try:
        cycle = 350.0
        input_rate = 5.0
        output_rate = 10.
        # 读取 CSV 文件
        df = pd.read_csv('output_t.csv')

        # 过滤出第 1 列等于 5 且第 2 列等于 10 的数据
        # filtered_df = df[(df.iloc[:, 2] == input_rate) & (df.iloc[:, 3] == output_rate)]

        # 计算第 4 列减去第 3 列的值
        y_values = df.iloc[:, 6]

        # 获取第 0 列作为 x 轴的值
        x_values = df.index

        # 确定点的颜色
        colors = np.where(y_values > 0, 'red', 'green')

        # 绘制折线图
        plt.figure(figsize=(10, 6))
        plt.plot(x_values, y_values, color='black', linestyle='-')  # 绘制折线

        # 计算平均值
        average_y = np.mean(y_values)

        # 绘制平均线
        plt.axhline(y=average_y, color='blue', linestyle='--', label=f'Average: {average_y:.2f}')

        plt.title(f'input rate{input_rate}, out rate {output_rate}')
        plt.xlabel('cycle')
        plt.ylabel('hit_m')
        plt.grid(True)
        plt.legend()
        plt.show()

    except FileNotFoundError:
        print("未找到 output_t.csv 文件，请检查文件路径。")
    except Exception as e:
        print(f"发生错误: {e}")


if __name__ == "__main__":
    path = f'data'
    os.chdir(path)
    filter_and_plot()